﻿package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;

	import flash.text.TextField;
	import flash.utils.Timer;
	
	// 麻雀素材まつセン
	// http://www4.cty-net.ne.jp/~l6000all/
	
	
	/**
	 * ...
	 * @author hato
	 */
	public class Main extends Sprite 
	{
		public var tf:TextField = new TextField();
		private var num:int = 1;
		public var haipai: HaipaiManager = new HaipaiManager();
		
		public var button: SimpleButton = new SimpleButton;
		public var button2: SimpleButton = new SimpleButton;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point	
			
			// -------
			// 牌の準備
			// -------
			var baseurl:String = "img/";
			haipai.LoadImages(baseurl);
			haipai.addEventListener(
				Event.COMPLETE, ImageLoadComplete);
			this.addChild(haipai);
			
			// -------
			// ボタン
			// -------
			button.x = 640;
			button.y = 10;
			button.upState   = makeRoundRect(0x228822);
			button.overState = makeRoundRect(0x225522);
			button.downState = makeRoundRect(0x553355);
			button.hitTestState = button.upState;
			button.addEventListener(MouseEvent.CLICK, onButtonClick);
			addChild(button);
			
			function onButtonClick(event:MouseEvent):void
			{
				//graphics.beginFill(0xFF0000, 0.5);
				//graphics.drawCircle(50, 50, 50);
				//graphics.endFill();
				ImageLoadComplete(event);
			}
			button2.x = 640;
			button2.y = 30;
			button2.upState   = makeRoundRect(0x228822);
			button2.overState = makeRoundRect(0x225522);
			button2.downState = makeRoundRect(0x553355);
			button2.hitTestState = button.upState;
			button2.addEventListener(MouseEvent.CLICK, onButton2Click);
			addChild(button2);
			
			function onButton2Click(event:MouseEvent):void
			{
				//graphics.beginFill(0xFF0000, 0.5);
				//graphics.drawCircle(50, 50, 50);
				//graphics.endFill();
				show_get();				
			}
			function makeRoundRect(color:uint):Sprite{
				var s:Sprite = new Sprite();
				s.graphics.beginFill(color);
				s.graphics.drawRoundRect(0, 0, 100, 20, 15);
				s.graphics.endFill();
				s.alpha = 0.8;
				return s;
			}
			
			
			
			tf.text = "たぬき";
			tf.x = 0;
			tf.y = 300;
			tf.height = 200;
			tf.width  = 400;
			//tf.addEventListener(
			//	MouseEvent.CLICK,tfOnClick);
			addChild(tf);
			
			haipai.addEventListener(MyEvent.HAI_CLICK, onHaiClick);
			
		}
		private function onHaiClick(e:MyEvent):void 
		{
			//trace("test event");
			//trace(haipai.hai);
			
			var str:String = haipai.hai;
			var ary:Array = str.split(",");
			
			//trace("sute=" + ary[e.v] + " caret=" + e.v);
			
			var c_ary:Array = 
				["cmd=sute", "caret=" + e.v, "bbb=1", "home=E"];
			var data:String = c_ary.join("\&");
			trace("senddata: " + data);
			
			//
			var request:URLRequest = new URLRequest(
				"http://192.168.1.11/~hato/bin/cgi-bin/interface.cgi?test=1\&" +
				data + "\&" +
				"random=" + Math.floor(Math.random() * 1000)//ブラウザキャッシュ対策
				);
			trace(request.url);
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onSentPost);
			loader.load(request);
		}
		
		private function onSentPost(e:Event):void 
		{
			var str:String = new String(e.target.data);
			//trace(str);
			tf.text = str;
			
			// 取得した牌をセットする
		}
		
		private function show_get():void 
		{
			var c_ary:Array = 
				["cmd=show","home=E"];
			var data:String = c_ary.join("\&");
			trace("senddata: " + data);
			//
			var request:URLRequest = new URLRequest(
				"http://192.168.1.11/~hato/bin/cgi-bin/interface.cgi?test=1\&" +
				data + "\&" +
				"random=" + Math.floor(Math.random() * 1000)//ブラウザキャッシュ対策
				);
			trace(request.url);
			request.method = URLRequestMethod.GET;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onSentPost);
			loader.load(request);
		}
		
		private function tfOnClick(event:Event):void
		{
			//haipai.tag = String(num);
			//num++;
			//if (num >= 10) { num = 1};
			ImageLoadComplete(event);
		}
		
		private function ImageLoadComplete( event:Event):void
		{
			trace("load complete");
			//
			// 情報取得
			// var request:URLRequest = new URLRequest(
			//	"http://192.168.1.11/~hato/bin/cgi-bin/test.cgi?What+is+Happens!");//
			//
			
			var request:URLRequest = new URLRequest(
				//"http://192.168.1.11/~hato/bin/cgi-bin/test.cgi?What+is+Happens!\&" +
				"http://192.168.1.11/~hato/bin/cgi-bin/interface.cgi?test=1\&" +
				"cmd=init&first=E&" + "random=" +
				Math.floor(Math.random() * 1000)//ブラウザキャッシュ対策
				);
			trace(request.url);
			request.method = URLRequestMethod.GET;
			var xmlloader:URLLoader = new URLLoader();
			xmlloader.addEventListener(Event.COMPLETE, onXMLLoad);
			xmlloader.load(request);	
		}
		
		//
		// <xml>
		//	<EAST>m:7,m:7,s:4,s:5,s:5,s:6,s:6,s:7,p:1,p:8,p:9,j:2</EAST>
		//	<RANDOM>$str</RANDOM>
		//	</xml>
		//
		private	function onXMLLoad(event:Event):void
		{
			var xml:XML = new XML(event.target.data);
			//trace(xml);
			tf.text = xml;
			
			var east:String = xml.child("EAST");
			trace("east " + east);
			
			var tsumo:String = xml.child("TSUMO");
			trace("tsumo " + tsumo);
			
			haipai.hai = east;
			haipai.tsumo = tsumo;
			
			/*
			var ary:Array = east.split(",");
			for (var cnt:String in ary) {
				var unit:String = ary[cnt];// m:5
				var unit_ary:Array = unit.split(":");
				var type: String = unit_ary[0];// m
				var num:  String = unit_ary[1];// 5
				//trace(cnt  + type + num);
			}
			*/
		}

	}
}	
	/**
	 * ...
	 * @author hato
	 */
/*	public class Main extends Sprite 
	{
		public var tf:TextField = new TextField();
		
		// 各牌のイメージを保持
		private var img_manz:Array = new Array();
		private var img_souz:Array = new Array();
		private var img_pinz:Array = new Array();
		
		// 作業イメージ
		private var sprite_ary:Array = new Array();
		private var bitmap_ary:Array = new Array();
		
		// 読み込んだ数
		private var n_request_all:int = 0;
		private var n_request_now:int = 0;
			
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point			
			tf.x = 150;
			tf.text = "text";
			this.addChild(tf);
			
			for (var i:int = 1; i <= 2;i++) {
				var baseurl:String = "img/";
				var str:String = baseurl + "man" + i + "u.gif";		
				var imgloader:Loader = new Loader();
				imgloader.contentLoaderInfo.addEventListener(
					Event.COMPLETE, onSub("man", i));
				
				function onSub(type:String, i:int) :Function {  
					return function (e:Event):void{;  
					//trace(e.target.name);  
						
					OnImageLoadComplete(e, type, i);
					} ;  
				}
				imgloader.load(new URLRequest(str));
				n_request_all++;
			}
			
			//
			// 表示用スプライトセット。移動しないので固定位置。
			//
			for (var j:int = 0; j <= 11; j++) {
				var sprite:Sprite = new Sprite();
				var bitmap:Bitmap = new Bitmap();
				//sprite.x = j * 30;
				
				bitmap_ary[j] = bitmap;
				sprite_ary[j] = sprite;
				sprite.addChild(bitmap);
				this.addChild(sprite);
			}
		}
		private function OnImageLoadComplete(event:Event, type:String, i:int):void 
		{
			var loader:Loader = Loader(event.target.loader);
			var bitmap:Bitmap = Bitmap(loader.content);
			//trace(bitmap);
			//trace(type + String(i));
				
			switch(type) {
				case "man":
					img_manz[i] = bitmap;
					break;
				case "sou":
					img_souz[i] = bitmap;
					break;
				case "pin":
					img_pinz[i] = bitmap;
					break;
				default:
					//trace("other");
					img_manz[i] = bitmap;
			}
			
			//img_manz.push(img as Sprite);
			//var sprite:Sprite = img as Sprite;
			//sprite.x = i * 30;
			//sprite.y = int(cnt) * 30;
			//addChild(sprite);
			n_request_now++;
			if (n_request_all == n_request_now) {
				trace("img complete!");
				//LoadXMLData(event);
				var p_sprite:Sprite = sprite_ary[0];
				var p_bitmap:Bitmap = bitmap_ary[0];
				var bitmap_index:int = p_sprite.getChildIndex(p_bitmap);
				trace("sprite contains is "      + p_sprite.contains(p_bitmap));
				trace("sprite child object num " + p_sprite.numChildren);
				trace("sprite bitmap index "     + String(bitmap_index));
				trace("sprite child 0 is "       + p_sprite.getChildAt(0));
				
				p_sprite.removeChildAt(bitmap_index);
				if (p_bitmap.bitmapData != null) {
					// bitmapdata なしなので通らないハズ
					trace("bitmap dispose");
					p_bitmap.bitmapData.dispose();
				}
				
				// bitmap コピー
				trace("-> bitmap copy")
				var src_bitmap:Bitmap = img_manz[1];
				var bitmapData:BitmapData = src_bitmap.bitmapData.clone();
				var cp_bitmap:Bitmap = new Bitmap(bitmapData);
				p_sprite.addChild(cp_bitmap);
				trace("sprite child object num " + p_sprite.numChildren);
				//trace("sprite bitmap index " + p_sprite.getChildIndex(p_bitmap));//p_bitmap未更新のためエラー。
				p_bitmap = cp_bitmap;
				trace("sprite bitmap index " + p_sprite.getChildIndex(p_bitmap));
				
				
				// 二回目。
				var bitmap_index2:int = p_sprite.getChildIndex(p_bitmap);
				p_sprite.removeChildAt(bitmap_index2);
				if (p_bitmap.bitmapData != null) {
					trace("bitmap dispose");
					p_bitmap.bitmapData.dispose();
				}
				// bitmap コピー
				trace("-> bitmap copy")
				var src_bitmap2:Bitmap = img_manz[2];
				var bitmapData2:BitmapData = src_bitmap2.bitmapData.clone();
				var cp_bitmap2:Bitmap = new Bitmap(bitmapData2);
				p_sprite.addChild(cp_bitmap2);
				p_bitmap = cp_bitmap2;
				trace("sprite child object num " + p_sprite.numChildren);
				trace("bitmap parent " + p_bitmap.parent);
			}
		}
	}
}
/*
			//
			// 牌画像読み込み
			// var baseurl:String = "http://192.168.1.11/~hato/bin/img/";
			//
			var baseurl:String = "img/";
			var h_type:Array = ["man", "sou", "pin"];
			
			for (var cnt:String in h_type) {
				
				var type:String = h_type[cnt];
				for (var i:int = 1; i <= 9; i++) {
					
					var str:String = baseurl + type + i + "u.gif";		
					var imgloader:Loader = new Loader();
					imgloader.contentLoaderInfo.addEventListener(
						Event.COMPLETE, onSub(type, i));
					
					function onSub(type:String, i:int) :Function {  
						return function (e:Event):void{;  
						//trace(e.target.name);  
							
						OnImageLoadComplete(e, type, i);
						} ;  
					}
					
					imgloader.load(new URLRequest(str));
					n_request_all++;
				}
			}
			
			//
			// 表示用スプライトセット。移動しないので固定位置。
			//
			for (var j:int = 0; j <= 11; j++) {
				var sprite:Sprite = new Sprite();
				var bitmap:Bitmap = new Bitmap();
				//sprite.x = j * 30;
				
				bitmap_ary[j] = bitmap;
				sprite_ary[j] = sprite;
				sprite.addChild(bitmap);
			}
			
			//
			// 手動リロードするボタン
			//
			var reload_btn:Sprite = new Sprite();
			reload_btn.graphics.beginFill(0xcc0000);
			reload_btn.graphics.drawRoundRect( -100, -25, 200, 50, 30);
			reload_btn.graphics.endFill();
			reload_btn.x = 150;
			reload_btn.y = 150;
			reload_btn.addEventListener(flash.events.MouseEvent.CLICK, this.LoadXMLData);
			this.stage.addChild(reload_btn);
		}
		
		//
		// <xml>
		//	<EAST>m:7,m:7,s:4,s:5,s:5,s:6,s:6,s:7,p:1,p:8,p:9,j:2</EAST>
		//	<RANDOM>$str</RANDOM>
		//	</xml>
		//
		private	function onXMLLoad(event:Event):void
		{
		   var xml:XML = new XML(event.target.data);
		   //trace(xml);
		   
		   var east:String = xml.child("EAST");
		   //trace("east=" +east);
		   
			//var line:String = "m:5,s:8,p:3";
			var ary:Array = east.split(",");
			for (var cnt:String in ary) {
				var unit:String = ary[cnt];// m:5
				var unit_ary:Array = unit.split(":");
				var type: String = unit_ary[0];// m
				var num:  String = unit_ary[1];// 5
				//trace(cnt  + type + num);
				
				var sprite:Sprite = sprite_ary[cnt] as Sprite;
				var bitmap:Bitmap = bitmap_ary[cnt] as Bitmap;
				var src_bitmap: Bitmap;
				switch (type) {
					case "m":
						src_bitmap = img_manz[num];
						break;
					case "s":
						src_bitmap = img_souz[num];
						break;
					case "p":
						src_bitmap = img_pinz[num];
						break;
					default:
						src_bitmap = img_manz[num];
						;
				}
				
				// bitmap コピー
				var bitmapData:BitmapData = src_bitmap.bitmapData.clone();
				var cp_bitmap:Bitmap = new Bitmap(bitmapData);
				bitmap = new Bitmap(bitmapData);
				//sprite.addChild(cp_bitmap);
				//bitmap = cp_bitmap;
				//addChild(sprite);
				
				// 大きさ
				sprite.scaleX = 2.0;
				sprite.scaleY = 2.0;
				sprite.x = int(cnt) * 30;
				sprite.buttonMode = true; // カーソル変更
				
				//
				// 表示＆マウスクリック時のイベント定義
				//
				//
				sprite.addEventListener(flash.events.MouseEvent.CLICK,onClick3(cnt, unit));  
				function onClick3(cnt: String, str:String) :Function{
					return function (e:MouseEvent):void{;  
					//trace("str",str);
					//trace(e.target.name);  
					
					trace("click cnt=" +cnt + " str=" +str);
					var sprite:Sprite = sprite_ary[cnt];
					if (sprite.getChildAt(0) is Bitmap) {
						var bitmap:Bitmap = sprite.getChildAt(0) as Bitmap;
						bitmap.bitmapData.dispose();
						sprite.removeEventListener(MouseEvent.CLICK, arguments.callee);
					}
					//onClick(cnt, str);
					} ;  
				}
				this.addChild(sprite);
				
			}
		}
		
		
		private function OnImageLoadComplete(event:Event, type:String, i:int):void 
		{
			var loader:Loader = Loader(event.target.loader);
			var bitmap:Bitmap = Bitmap(loader.content);
			//trace(bitmap);
			//trace(type + String(i));
				
			switch(type) {
				case "man":
					img_manz[i] = bitmap;
					break;
				case "sou":
					img_souz[i] = bitmap;
					break;
				case "pin":
					img_pinz[i] = bitmap;
					break;
				default:
					//trace("other");
					img_manz[i] = bitmap;
			}
			
			//img_manz.push(img as Sprite);
			//var sprite:Sprite = img as Sprite;
			//sprite.x = i * 30;
			//sprite.y = int(cnt) * 30;
			//addChild(sprite);
			n_request_now++;
			if (n_request_all == n_request_now) {
				trace("img complete!");
				LoadXMLData(event);
			}
		}
		
		
		private function LoadXMLData(event:Event) : void
		{	
			//
			// 情報取得
			// var request:URLRequest = new URLRequest(
			//	"http://192.168.1.11/~hato/bin/cgi-bin/test.cgi?What+is+Happens!");//
			//
			var xmlloader:URLLoader = new URLLoader();
			var url:URLRequest = new URLRequest(
				//"http://192.168.1.11/~hato/bin/cgi-bin/test.cgi?What+is+Happens!\&" +
				"http://192.168.1.11/~hato/bin/cgi-bin/interface.cgi?test=1\&" +
				Math.floor(Math.random() * 1000)//ブラウザキャッシュ対策
				);
			xmlloader.addEventListener(Event.COMPLETE, onXMLLoad);
			xmlloader.load(url);		
		}
		
		//private function onClick(cnt:String, str:String): void
		//{
		//	trace("click cnt=" +cnt + " str=" +str);
		//	var sprite:Sprite = sprite_ary[cnt];
		//	var bitmap:Bitmap = sprite.getChildAt(0) as Bitmap;
		//	bitmap.bitmapData.dispose();
		//}
		
	}
}


/*
 * timer 
 * 
			// エラー時の処理
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			loader.load(request);
			
		public var timer:Timer = new Timer(1000);
			this.timer.addEventListener(TimerEvent.TIMER, tick01);
			this.timer.start();
 		private function ioErrorHandler( event:IOErrorEvent):void
		{
			trace("io error");
		}
		public function tick01( event:TimerEvent ):void
		{
			var ran: int = Math.random()*5;
			this.tf.text = String(ran);
			trace(ran);
		}
*/


